/**
 * Settle模块 - 分账规则管理API
 * @description 分账规则管理相关的API接口定义
 * @author AI Assistant
 * @since 1.0.0
 */

import request from '@/config/axios'
import type {
  SplitRuleVO,
  SplitRuleCreateReqVO,
  SplitRuleUpdateReqVO,
  SplitRulePageReqVO,
  SplitRecordVO,
  PageResult
} from './types'

/**
 * 获取分账规则分页列表
 * @param params 分页查询参数
 * @returns 分账规则分页数据
 */
export const getSplitRulePageApi = (
  params: SplitRulePageReqVO
): Promise<PageResult<SplitRuleVO>> => {
  return request.get({ url: '/settle/split-rule/page', params })
}

/**
 * 获取分账规则详情
 * @param id 规则ID
 * @returns 分账规则详情
 */
export const getSplitRuleApi = (id: number): Promise<SplitRuleVO> => {
  return request.get({ url: `/settle/split-rule/get?id=${id}` })
}

/**
 * 创建分账规则
 * @param data 分账规则创建数据
 * @returns 创建结果
 */
export const createSplitRuleApi = (data: SplitRuleCreateReqVO): Promise<number> => {
  return request.post({ url: '/settle/split-rule/create', data })
}

/**
 * 更新分账规则
 * @param data 分账规则更新数据
 * @returns 更新结果
 */
export const updateSplitRuleApi = (data: SplitRuleUpdateReqVO): Promise<boolean> => {
  return request.put({ url: '/settle/split-rule/update', data })
}

/**
 * 删除分账规则
 * @param id 规则ID
 * @returns 删除结果
 */
export const deleteSplitRuleApi = (id: number): Promise<boolean> => {
  return request.delete({ url: `/settle/split-rule/delete?id=${id}` })
}

/**
 * 启用分账规则
 * @param id 规则ID
 * @returns 启用结果
 */
export const enableSplitRuleApi = (id: number): Promise<boolean> => {
  return request.put({ url: `/settle/split-rule/enable?id=${id}` })
}

/**
 * 禁用分账规则
 * @param id 规则ID
 * @returns 禁用结果
 */
export const disableSplitRuleApi = (id: number): Promise<boolean> => {
  return request.put({ url: `/settle/split-rule/disable?id=${id}` })
}

/**
 * 获取商户的分账规则列表
 * @param merchantNo 商户编号
 * @returns 分账规则列表
 */
export const getSplitRuleByMerchantApi = (merchantNo: string): Promise<SplitRuleVO[]> => {
  return request.get({ url: `/settle/split-rule/list-by-merchant?merchantNo=${merchantNo}` })
}

/**
 * 获取分账记录分页列表
 * @param params 分页查询参数
 * @returns 分账记录分页数据
 */
export const getSplitRecordPageApi = (params: {
  pageNo: number
  pageSize: number
  orderNo?: string
  merchantNo?: string
  splitStatus?: number
  createTime?: [string, string]
}): Promise<PageResult<SplitRecordVO>> => {
  return request.get({ url: '/settle/split-record/page', params })
}

/**
 * 手动执行分账
 * @param orderNo 订单号
 * @returns 分账结果
 */
export const executeSplitApi = (orderNo: string): Promise<boolean> => {
  return request.post({ url: '/settle/split-record/execute', data: { orderNo } })
}

/**
 * 导出分账规则数据
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportSplitRuleApi = (params: SplitRulePageReqVO): Promise<void> => {
  return request.download({
    url: '/settle/split-rule/export',
    params
  })
}

/**
 * 导出分账记录数据
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportSplitRecordApi = (params: {
  orderNo?: string
  merchantNo?: string
  splitStatus?: number
  createTime?: [string, string]
}): Promise<void> => {
  return request.download({
    url: '/settle/split-record/export',
    params
  })
}
